#delimit ;

drop _all;
set linesize 200;

quietly {;
  capture log close;
  log using table_simulation.tex, text replace;

  noisily display "\documentclass[12 pt]{report}";
  noisily display "\pagestyle{plain}";
  noisily display "\setlength{\topmargin}{-.50in}";
  noisily display "\setlength{\textheight}{8.9in}";
  noisily display "\setlength{\oddsidemargin}{0in}";
  noisily display "\setlength{\evensidemargin}{0in}";
  noisily display "\setlength{\textwidth}{6.5in}";
  noisily display "\begin{document}";
  noisily display "";

  log off;
};


foreach type in normal skewed bimodal {;

  foreach k in 0 5 {;

    use tmp_simulations_`type'_`k', clear;
    drop if b_1==.;

    foreach i in 1 2 3 4 5 40 {;

      sum b_`i';
      local m`k'_1_`i' = _result(3);
      local s`k'_1_`i' = sqrt(sqrt(_result(4)));
      gen x = (p_`i' < .05);
      sum x;
      local p`k'_1_`i' = _result(3);
      drop x;

      sum b_poly_`i';
      local m`k'_2_`i' = _result(3);
      local s`k'_2_`i' = sqrt(_result(4));
      gen x = p_poly_`i' < .05;
      sum x;
      local p`k'_2_`i' = _result(3);
      drop x;

      sum b_lin_`i';
      local m`k'_3_`i' = _result(3);
      local s`k'_3_`i' = sqrt(_result(4));
      gen x = p_lin_`i' < .05;
      sum x;
      local p`k'_3_`i' = _result(3);
      drop x;

      sum b_diff2_`i';
      local m`k'_5_`i' = _result(3);
      local s`k'_5_`i' = sqrt(_result(4));
      gen x = p_diff2_`i' < .05;
      sum x;
      local p`k'_5_`i' = _result(3);
      drop x;

    };

    sum b_optimal_bandwidth;
    local m`k'_3_6  = _result(3);
    local s`k'_3_6  = sqrt(_result(4));
    gen x = 1 - c_optimal_bandwidth;
    sum x;
    local p`k'_3_6  = _result(3);
    sum optimal_bandwidth;
    local o`k'_3_6  = _result(3);
    drop x;

  };


  quietly {;

    log on;

    noisily display "\newpage";
    noisily display "\begin{center}\setlength{\baselineskip}{12pt}";
    noisily display "\begin{tabular}{|l|c|c|c|c|c|c|c|}";
    noisily display "\hline";
    noisily display "\multicolumn{8}{|c|}{} \\ [-.08in]";
    noisily display "\multicolumn{8}{|c|}{\bf Table 1: Simulation Results} \\ [.10in]";
    noisily display "\hline";
    noisily display "\multicolumn{8}{|c|}{} \\ [-.12in]";
    noisily display "\multicolumn{8}{|c|}{`type' Distribution of Normal Votes} \\ [.05in] ";
    noisily display "\hline";
    noisily display "& \multicolumn{7}{|c|}{} \\ [-.12in]";
    noisily display "& \multicolumn{7}{|c|}{Margin Defining Window} \\ [.05in]";
    noisily display "\cline{2-8}";
    noisily display "& & & & & & & \\ [-.12in]";
    noisily display "Specification & 1\% & 2\% & 3\% & 4\% & 5\% & 40\% & OBW \\ [.05in]";
    noisily display "\hline";
    noisily display "\multicolumn{8}{|c|}{} \\ [-.12in]";
    noisily display "\multicolumn{8}{|c|}{True Incumbency Advantage = 0} \\ [.05in]";
    noisily display "\hline";
    noisily display "& & & & & & & \\ [-.12in]";
    noisily display "Baseline		&  " %4.2f `m0_1_1' "  &  " %4.2f `m0_1_2' "  &  " %4.2f `m0_1_3' "  &  " %4.2f `m0_1_4' "  &  " %4.2f `m0_1_5' "  &  "                 "  &  "                     "  \\";
    noisily display "			& (" %4.2f `s0_1_1' ") & (" %4.2f `s0_1_2' ") & (" %4.2f `s0_1_3' ") & (" %4.2f `s0_1_4' ") & (" %4.2f `s0_1_5' ") &  "                 "  &  "                     "  \\";
    noisily display "			& [" %4.2f `p0_1_1' "] & [" %4.2f `p0_1_2' "] & [" %4.2f `p0_1_3' "] & [" %4.2f `p0_1_4' "] & [" %4.2f `p0_1_5' "] &  "                 "  &  "                     "  \\ [.05in]";
    noisily display "Polynomial Control	&  "                "  &  "                "  &  "                "  &  "                "  &  " %4.2f `m0_2_5' "  &  " %4.2f `m0_2_40' "  &  "                     "  \\";
    noisily display "			&  "                "  &  "                "  &  "                "  &  "                "  & (" %4.2f `s0_2_5' ") & (" %4.2f `s0_2_40' ") &  "                     "  \\";
    noisily display "			&  "                "  &  "                "  &  "                "  &  "                "  & [" %4.2f `p0_2_5' "] & [" %4.2f `p0_2_40' "] &  "                     "  \\ [.05in]";
    noisily display "Local Linear Control	&  " %4.2f `m0_3_1' "  &  " %4.2f `m0_3_2' "  &  " %4.2f `m0_3_3' "  &  " %4.2f `m0_3_4' "  &  " %4.2f `m0_3_5' "  &  "                 "  &  "     %4.2f `m0_3_6' "  \\";
    noisily display "			& (" %4.2f `s0_3_1' ") & (" %4.2f `s0_3_2' ") & (" %4.2f `s0_3_3' ") & (" %4.2f `s0_3_4' ") & (" %4.2f `s0_3_5' ") &  "                 "  & ("     %4.2f `s0_3_6' ") \\";
    noisily display "			& [" %4.2f `p0_3_1' "] & [" %4.2f `p0_3_2' "] & [" %4.2f `p0_3_3' "] & [" %4.2f `p0_3_4' "] & [" %4.2f `p0_3_5' "] &  "                 "  & ["     %4.2f `p0_3_6' "] \\";
    noisily display "                     &  "                "  &  "                "  &  "                "  &  "                "  &  "                "  &  "                 "  & {\it " %4.2f `o0_3_6' "} \\ [.05in]";
    noisily display "\hline";

    noisily display "\multicolumn{8}{|c|}{} \\ [-.12in]";
    noisily display "\multicolumn{8}{|c|}{True Incumbency Advantage = 5} \\ [.05in]";
    noisily display "\hline";
    noisily display "& & & & & & & \\ [-.12in]";
    noisily display "Baseline		&  " %4.2f `m5_1_1' "  &  " %4.2f `m5_1_2' "  &  " %4.2f `m5_1_3' "  &  " %4.2f `m5_1_4' "  &  " %4.2f `m5_1_5' "  &  "                 "  &  "                     "  \\";
    noisily display "			& (" %4.2f `s5_1_1' ") & (" %4.2f `s5_1_2' ") & (" %4.2f `s5_1_3' ") & (" %4.2f `s5_1_4' ") & (" %4.2f `s5_1_5' ") &  "                 "  &  "                     "  \\";
    noisily display "			& [" %4.2f `p5_1_1' "] & [" %4.2f `p5_1_2' "] & [" %4.2f `p5_1_3' "] & [" %4.2f `p5_1_4' "] & [" %4.2f `p5_1_5' "] &  "                 "  &  "                     "  \\ [.05in]";
    noisily display "Polynomial Control	&  "                "  &  "                "  &  "                "  &  "                "  &  " %4.2f `m5_2_5' "  &  " %4.2f `m5_2_40' "  &  "                     "  \\";
    noisily display "			&  "                "  &  "                "  &  "                "  &  "                "  & (" %4.2f `s5_2_5' ") & (" %4.2f `s5_2_40' ") &  "                     "  \\";
    noisily display "			&  "                "  &  "                "  &  "                "  &  "                "  & [" %4.2f `p5_2_5' "] & [" %4.2f `p5_2_40' "] &  "                     "  \\ [.05in]";
    noisily display "Local Linear Control	&  " %4.2f `m5_3_1' "  &  " %4.2f `m5_3_2' "  &  " %4.2f `m5_3_3' "  &  " %4.2f `m5_3_4' "  &  " %4.2f `m5_3_5' "  &  "                 "  &  "     %4.2f `m5_3_6' "  \\";
    noisily display "			& (" %4.2f `s5_3_1' ") & (" %4.2f `s5_3_2' ") & (" %4.2f `s5_3_3' ") & (" %4.2f `s5_3_4' ") & (" %4.2f `s5_3_5' ") &  "                 "  & ("     %4.2f `s5_3_6' ") \\";
    noisily display "			& [" %4.2f `p5_3_1' "] & [" %4.2f `p5_3_2' "] & [" %4.2f `p5_3_3' "] & [" %4.2f `p5_3_4' "] & [" %4.2f `p5_3_5' "] &  "                 "  & ["     %4.2f `p5_3_6' "] \\";
    noisily display "                     &  "                "  &  "                "  &  "                "  &  "                "  &  "                "  &  "                 "  & {\it " %4.2f `o5_3_6' "} \\ [.05in]";
    noisily display "\hline";

    noisily display "\end{tabular}\end{center}";
    noisily display "";
    noisily display "\setlength{\baselineskip}{15pt} \noindent The cell entries show the average estimated value of ";
    noisily display "\textit{Incumbency Advantage} ($\beta_1$).  The standard deviation of the estimated values are in ";  
    noisily display "parentheses.  The fraction of cases in which H0: $\hat\beta_1 = \beta_1^{TRUE}$ is rejected at the ";
    noisily display ".05 level are in brackets.  The term in italics is the average bandwidth chosen by the Imbens and ";
    noisily display "Kalyanaraman procedure.";
    noisily display "";

    log off;

  };

};

quietly {;
  log on;
  noisily display "\end{document}";
  log close;
};

